<?php

namespace app\cms\admin;

use app\system\admin\Admin;
use app\system\model\SystemArticle;
use think\Db;


class Index extends Admin
{
    protected $hisiModel = '';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function index($group = 'base')
    {
        if ($this->request->isAjax()) {
            $where  = $data = [];
            $page   = $this->request->param('page/d', 1);
            $limit  = $this->request->param('limit/d', 15);

            if ($group) {
                $where['group'] = $group;
            }
            $data["data"] = Db::table("hisi_article")->page($page)->limit($limit)->select();
            $data['count']  = Db::table("hisi_article")->count('postId');
            $data['code']   = 0;
            return json($data);
        }

        return $this->fetch();
    }

    /**
     * 增加页面
     * @return mixed|string|void
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $data = $this->request->post();

            $data["imgSrc"] = "/images/" . $data["imgSrc"];

            $data["create_time"] = time();
            $data["update_time"] = time();
            $data["delete_time"] = 0;

            $result = Db::table("hisi_swiper")->insert($data);

            if ($result){
                return $this->success("成功");
            } else {
                return $this->error("失败");
            }
        }

        return $this->fetch('add');
    }

    public function edit()
    {
        $data = $this->request->param();

        $id = intval($data["postId"]);
        $status = intval($data["status"]);

        if ($status == 1) {
            $status = 10;
        } else if ($status == 10) {
            $status = 1;
        }

        $modle = new SystemArticle();

        $result = $modle::where("postId",$id)->update(["status" => $status]);

        if ($result) {
            return $this->success('修改成功');
        }

        return $this->error("修改失败");
    }

    /**
     * @return mixed|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $id = $this->request->param('postId/a');

        //$result = Db::table("swiper")->where("postId",intval($id[0]))->update(["delete_time" => time()]);
        $result = Db::table("hisi_swiper")->where("postId",intval($id[0]))->delete();

        if ($result) {
            return $this->success('删除成功');
        }

        return $this->error("删除失败");
    }
}